// Package avatar -----------------------------
// @file      : avatarResourceModule.go
// @author    : WuZhouHai
// @contact   : wzh200x@126.com
// @time      : 2024/7/4 16:57
// @desc      :
// -------------------------------------------
package avatar

import (
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"phoenix/apps/examples/phoenix_app_dwys/defines"
	"phoenix/apps/examples/phoenix_app_dwys/defines/entityType"
	"phoenix/apps/examples/phoenix_app_dwys/model/entityModule/avatar"
	"phoenix/phoenix/defines/dataType"
	"phoenix/phoenix/entitys/entityModule"
	"phoenix/phoenix/interfaces"
	"phoenix/phoenix/utils"
)

type AvatarResourceModule struct {
	entityModule.EntityModuleBase
}

func (this *AvatarResourceModule) Init() {
	//this.HarvestCarrot()
}

func (this *AvatarResourceModule) GetResources() (*avatar.AvatarResourcesModelData, error) {
	model := this.GetModuleModel()
	if model == nil {
		return nil, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_MODULE_DATA_NOT_EXIST, "", nil),
			"avatar resource module data not exist")
	}
	if resModuleModel, ok := model.(*avatar.AvatarResourcesModel); ok {
		resModuleModelData := resModuleModel.GetModelData(0).(*avatar.AvatarResourcesModelData)
		return resModuleModelData, nil
	}
	return nil, gerror.NewCodef(gcode.New(defines.CUSTOMER_ERROR_RES_DATA_ERROR, "", nil),
		"res data error")
}

func (this *AvatarResourceModule) CheckResource(resName string, value int32) bool {
	resModuleModel := this.GetModuleModel().(*avatar.AvatarResourcesModel)
	resModuleModelData := resModuleModel.GetModelData(0).(*avatar.AvatarResourcesModelData)
	switch resName {
	case defines.GameResType_Carrot:
		return resModuleModelData.Carrot >= value
	case defines.GameResType_Clover:
		return resModuleModelData.Clover >= value
	case defines.GameResType_GoldCarrot:
		return resModuleModelData.GoldCarrot >= value
	case defines.GameResType_Star:
		return resModuleModelData.Star >= value
	}
	return false
}

func (this *AvatarResourceModule) ChangeResource(resName string, change int32) {
	resModuleModel := this.GetModuleModel().(*avatar.AvatarResourcesModel)
	resModuleModelData := resModuleModel.GetModelData(0).(*avatar.AvatarResourcesModelData)
	switch resName {
	case defines.GameResType_Carrot:
		resModuleModelData.Carrot += change
	case defines.GameResType_Clover:
		resModuleModelData.Clover += change
	case defines.GameResType_GoldCarrot:
		resModuleModelData.GoldCarrot += change
	case defines.GameResType_Star:
		resModuleModelData.Star += change
	}
	resModuleModel.SetUpdateSingleData()
}

func (this *AvatarResourceModule) ChangeMinuteCarrot(change int32) {
	resModuleModel := this.GetModuleModel().(*avatar.AvatarResourcesModel)
	resModuleModelData := resModuleModel.GetModelData(0).(*avatar.AvatarResourcesModelData)
	if resModuleModelData.MinuteCarrot == 0 { // 首次变动,重置一下时间
		resModuleModelData.LastHarvest = int32(utils.GetNowTimestampSecond())
	}
	resModuleModelData.MinuteCarrot += change
	resModuleModel.SetUpdateSingleData()
}

func (this *AvatarResourceModule) HarvestCarrot() int32 {
	resModuleModel := this.GetModuleModel().(*avatar.AvatarResourcesModel)
	data := resModuleModel.GetModelData(0)
	if data == nil {
		return 0
	}
	resModuleModelData := resModuleModel.GetModelData(0).(*avatar.AvatarResourcesModelData)
	if resModuleModelData.LastHarvest == 0 {
		resModuleModelData.LastHarvest = int32(utils.GetNowTimestampSecond())
		return 0
	}
	if resModuleModelData.MinuteCarrot == 0 {
		return 0
	}
	expendTime := (utils.GetNowTimestampSecond() - int64(resModuleModelData.LastHarvest)) / 60
	if expendTime == 0 {
		return 0
	}
	rewardTime := expendTime
	if rewardTime > 60*24 {
		rewardTime = 60 * 24
	}
	getCarrot := int32(rewardTime) * resModuleModelData.MinuteCarrot
	resModuleModelData.Carrot += getCarrot
	resModuleModelData.LastHarvest += int32(expendTime) * 60
	resModuleModel.SetUpdateSingleData()
	return getCarrot
}

func NewAvatarResourceModule(entityDBID dataType.PhoenixTypeDataBaseId) interfaces.IEntityModule {
	return &AvatarResourceModule{
		EntityModuleBase: entityModule.NewEntityModuleBase(entityType.EntityName_Avatar,
			defines.ModuleName_Avatar_Resources, entityDBID),
	}
}
